from DataAccess.DBConnFactory import DBConnFactory
#from DataAccess.blp_client import query_blp_data

from datetime import date
from string import Template


def update_stock_static_info_from_vendor(ref_date):

	cursor = DBConnFactory().get_db_connection('FINCHINA').cursor()
	#sql_text = '''select exchange, symbol, sname, stype, currency from securitycode 
	sql_text = '''select symbol,sname from securitycode 
				where (exchange='CNSESH' or exchange='CNSESZ' or exchange='HKSE00')
					and (stype='EQA' or stype='EQB' or stype='HKE')
					and status=0 and enabled=0 '''
	cursor.execute(sql_text)
	#tmp = cursor.fetchall()
	tick_name_list = [(entry[0], entry[1].decode('gbk')) for entry in cursor.fetchall()]
	#print tick_name_list
	
	#
	#decode: convert other code to unicode
	#encode: convert unicode to other code

	pke_conn = DBConnFactory().get_db_connection('PKEDB')
	pke_conn.cursor().executemany('''merge into comm_security_static_info s
							using (select :1 ticker,:2 nm from dual) t
								on (t.ticker=s.ticker)
							when matched then update set
								s."name"=t.nm
							when not matched then insert
								values(t.ticker,t.nm)''', tick_name_list)
	pke_conn.commit()
	return